﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;

using TravelingWebApp.Middleware;
using TravelingWebApp.Controllers;
using TravelingWebApp.Models;
using TravelingWebApp.ViewModels;
using TravelingWebApp.Data;
using TravelingWebApp.Repositories;

using System.Security.Claims;

namespace TravelingWebApp
{
    public class Startup
    {
        private IConfiguration _configuration;

        public Startup(IConfiguration conf)
        {
            _configuration = conf;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //跨域
            //services.AddCors(options =>
            // options.AddPolicy("AllowAll", p => p.WithOrigins("http://localhost:1857")
            // .AllowAnyMethod()
            // .AllowAnyHeader()
            // .AllowCredentials()
            // ));
            services.AddCors(options => {
                options.AddPolicy("any", builder =>
                {
                    builder.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                });
            });

            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            
            //User相关
            services.AddIdentity<User, IdentityRole>(options =>
            {
                options.Password.RequireDigit = true;
                options.Password.RequiredLength = 7;
                options.Password.RequireUppercase = false;
                options.User.RequireUniqueEmail = false;
            })
            .AddEntityFrameworkStores<TravelingContext>();

            //数据库上下文
            services.AddDbContext<TravelingContext>(options =>
                options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")));

            //授权设置
            services.AddAuthorization(options =>
            {
                options.AddPolicy("RequireEmail", policy => policy.RequireClaim(ClaimTypes.Email));
            });

            

            //123qwe!@#QWE
            services.AddTransient<IScenaryRepositories, ScenaryRepositories>();

            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, TravelingContext ctx)
        {
            //允许数据库 每次运行都可以删除和创建数据库 真有你的的啊
            //ctx.Database.EnsureDeleted();
            //ctx.Database.EnsureCreated();

            app.UseCors("any");

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseAuthentication();
            app.UseNodeModules(env.ContentRootPath);
            

            app.UseMvc();
        }
    }
}
